import axios from "axios";
import {dictionary} from "../utils/allDictionary";

let buyerMin = {
  methods:{
    format(scope,type){
      if(type === 0){
        for(let i=0 ; i<dictionary.serviceKind.length; i++)
        {
          if(dictionary.serviceKind[i].key === scope.row.or_kind){
            return dictionary.serviceKind[i].name
          }
        }
      }

      if(type === 1)
      {
        for(let i=0 ; i<dictionary.orderStatus.length; i++)
        {
          if(dictionary.orderStatus[i].key === scope.row.or_type){
            return dictionary.orderStatus[i].name
          }
        }
      }

      if(type === 2){
        for(let i=0 ; i<dictionary.acceptStatus.length; i++)
        {
          if(dictionary.acceptStatus[i].key === scope.row.or_accept){
            return dictionary.acceptStatus[i].name
          }
        }
      }

      if(type === 3){
        for(let i=0 ; i<dictionary.payStatus.length; i++)
        {
          if(dictionary.payStatus[i].key === scope.row.or_pay){
            return dictionary.payStatus[i].name
          }
        }
      }
    },
    accept(id,money) {
      this.id = id
      this.money = money
      this.pay = true
    },
    reject(id){
      this.id = id

      this.$confirm('此操作将取消该订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = 'https://haotry.top/api/buyer/cancelOrder'
        axios.post(url,JSON.stringify({or_id:this.id}),{
          headers:{
            'Content-Type':'application/json'
          }
        }).then(res => {
          let data = res.data
          console.log(data)
          if(data.status === 200){
            this.$message({
              type: 'success',
              message: '取消订单成功!'
            });
            this.reload()
          }
          else{
            this.$message({
              type: 'info',
              message: '取消订单失败!'
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已停止取消'
        })
      })
    },
    confirmPay(){
      let url = 'https://haotry.top/api/buyer/payOrder'
      axios.post(url,JSON.stringify({or_id:this.id}),{
        headers:{
          'Content-Type':'application/json'
        }
      }).then(res => {
        let data = res.data
        console.log(data)
        if(data.status === 200){
          this.$message({
            message: '支付成功',
            type: 'success'
          });
          this.pay = false
          this.reload()
        }
        else{

        }
      })
    },
    confirmFinish(id){
      this.$confirm('此操作将确认完成订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = 'https://haotry.top/api/buyer/finishOrder'
        axios.post(url,JSON.stringify({or_id:id}),{
          headers:{
            'Content-Type':'application/json'
          }
        }).then(res => {
          let data = res.data
          console.log(data)
          if(data.status === 200){
            this.$message({
              type: 'success',
              message: '订单确认成功!'
            });
            this.reload()
          }
          else{
            this.$message({
              type: 'info',
              message: '订单确认失败!'
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消确认'
        })
      })
    },
    commentOrder(id){
      this.or_id = id

      let url = 'https://haotry.top/api/buyer/getEvaByOrId'
      axios.post(url,JSON.stringify({or_id:id}),{
        headers:{
          'Content-Type':'application/json'
        }
      }).then(res => {
        let data = res.data
        console.log(data)
        if(data.status === 200){
          this.comments = data.data.eva_content
          this.commentStatus = 2
        }
        else if(data.status === 501){
          this.commentStatus = 1
        }
      })
      this.comment = true
    },
    changeComment(){
      this.comment = false
      if(this.commentStatus === 1){
        //发布评价
        let url = 'https://haotry.top/api/buyer/addEva'
        axios.post(url,JSON.stringify({or_id:this.or_id,eva_content : this.comments}),{
          headers:{
            'Content-Type':'application/json'
          }
        }).then(res => {
          let data = res.data
          console.log(data)
          if(data.status === 200){
            this.$message({
              message: '评论发布成功',
              type: 'success'
            });
          }
          else{
            this.$message({
              message: '评论发布失败',
              type: 'warning'
            });
          }
        })
      }
      else if(this.commentStatus === 2){
        //修改评价
        let url = 'https://haotry.top/api/buyer/updateEva'
        axios.post(url,JSON.stringify({or_id:this.or_id,eva_content:this.comments}),{
          headers:{
            'Content-Type':'application/json'
          }
        }).then(res => {
          let data = res.data
          console.log(data)
          if(data.status === 200){
            this.$message({
              message: '评论修改成功',
              type: 'success'
            });
          }
          else{
            this.$message({
              message: '评论修改失败',
              type: 'warning'
            });
          }
        })
      }
    },
    searchOrder(form){
      let url = 'https://haotry.top/api/buyer/getOrder'
      axios.post(url,JSON.stringify(form),{
        headers:{
          'Content-Type':'application/json'
        }
      }).then(res => {
        let data = res.data
        console.log(data)
        if(data.status === 200){
          this.tableData = data.data
        }
        else{
        }
      })
    },
    handleSizeChange(val) {
      this.size = val
    },
    handleCurrentChange(val) {
      this.currentPage = val
    }
  }
}

export default buyerMin